﻿using System;
using System.Windows.Input;

namespace XPathAutoSuggest {
    /// <summary>
    /// A simple class to allow a form to bind to a command that evaluates the users&apos; XPath
    /// </summary>
    public class EvaluateCommand : CommandBase {
        /// <summary>
        /// Constructor takes an instance of the view model hosting the guts of the code
        /// </summary>
        /// <param name="newVm">View model</param>
        public EvaluateCommand(IXPathToolVm newVm) : base(newVm) {
            _MV = newVm;
        }

        /// <summary>
        /// Whether the command can execute
        /// </summary>
        /// <param name="parameter">Not used</param>
        /// <returns>True if the command can fire</returns>
        public override bool CanExecute(object parameter) {
            return ((IMainModelView)_MV).EvaluateCommandCanExecute();
        }

        /// <summary>
        /// Does the actual work
        /// </summary>
        /// <param name="parameter">Optional XPath query to evaluate</param>
        public override void Execute(object parameter) {
            if (parameter is string)
                ((IMainModelView) _MV).EvaluateExecute(parameter as string);
            else
                ((IMainModelView) _MV).EvaluateExecute();
        }
    }

    /// <summary>
    /// A simple class to allow a form to bind to a command that Suggests an XPath query matching users selection
    /// </summary>
    public class SuggestCommand : CommandBase {

        /// <summary>
        /// Constructor takes an instance of the view model hosting the guts of the code
        /// </summary>
        /// <param name="newVm">View model</param>
        public SuggestCommand(IXPathToolVm newVm)
            : base(newVm) {
        }

        /// <summary>
        /// Whether the command can execute
        /// </summary>
        /// <param name="parameter">Not used</param>
        /// <returns>True if the command can fire</returns>
        public override bool CanExecute(object parameter) {
            return ((IMainModelView) _MV).SuggestCanExecute();
        }

        /// <summary>
        /// Does the actual work
        /// </summary>
        /// <param name="parameter">Not used</param>
        public override void Execute(object parameter) {
            ((IMainModelView) _MV).SuggestExecute();
        }
    }

    /// <summary>
    /// A simple class to allow a form to bind to a command that formats users' XML
    /// </summary>
    public class FormattCommand : CommandBase {
        /// <summary>
        /// Constructor takes an instance of the view model hosting the guts of the code
        /// </summary>
        /// <param name="newModelView">View model</param>
        public FormattCommand(IXPathToolVm newModelView) : base(newModelView) { }

        /// <summary>
        /// Whether the command can execute
        /// </summary>
        /// <param name="parameter">Not used</param>
        /// <returns>True if the command can fire</returns>
        public override bool CanExecute(object parameter) {
            return ((IMainModelView) _MV).FormatCommandCanExecute();
        }

        /// <summary>
        /// Does the actual work
        /// </summary>
        /// <param name="parameter">Not used</param>
        public override void Execute(object parameter) {
            ((IMainModelView) _MV).FormatCommandExecute();
        }
    }

    /// <summary>
    /// A simple class to allow a form to bind to a command that shows a form with links to XPath topics
    /// </summary>
    public class ShowLinksCommand : CommandBase {
        /// <summary>
        /// Constructor takes an instance of the view model hosting the guts of the code
        /// </summary>
        /// <param name="newModelView">View model</param>
        public ShowLinksCommand(IXPathToolVm newModelView) : base(newModelView) { }

        /// <summary>
        /// Whether the command can execute
        /// </summary>
        /// <param name="parameter">Not used</param>
        /// <returns>True if the command can fire</returns>
        public override bool CanExecute(object parameter) {
            return ((IMainModelView) _MV).ShowLinksCommandCanExecute();
        }

        /// <summary>
        /// Does the actual work
        /// </summary>
        /// <param name="parameter">Not used</param>
        public override void Execute(object parameter) {
            ((IMainModelView) _MV).ShowLinksCommandExecute();
        }
    }

    /// <summary>
    /// A simple class to allow a form to bind to a command that launches the about dialog
    /// </summary>
    public class AboutCommand : CommandBase {
        /// <summary>
        /// Constructor takes an instance of the view model hosting the guts of the code
        /// </summary>
        /// <param name="newModelView">View model</param>
        public AboutCommand(IXPathToolVm newModelView) : base(newModelView) { }

        /// <summary>
        /// Whether the command can execute
        /// </summary>
        /// <param name="parameter">Not used</param>
        /// <returns>True if the command can fire</returns>
        public override bool CanExecute(object parameter) {
            return ((IMainModelView) _MV).AboutCommandCanExecute();
        }

        /// <summary>
        /// Does the actual work
        /// </summary>
        /// <param name="parameter">Not used</param>
        public override void Execute(object parameter) {
            ((IMainModelView) _MV).AboutCommandExecute();
        }
    }

    /// <summary>
    /// A class to bind to a button and validate user XML
    /// </summary>
    public class ValidateXmlCommand : CommandBase {
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="newModelView">ModelView hosting the code</param>
        public ValidateXmlCommand(IXPathToolVm newModelView) : base(newModelView) { }

        /// <summary>
        /// Whether the command can fire
        /// </summary>
        /// <param name="parameter">Not used</param>
        /// <returns>True/false</returns>
        public override bool CanExecute(object parameter) {
            return ((IMainModelView)_MV).ValidateXmlCommandCanExecute();
        }

        /// <summary>
        /// Does the actual work
        /// </summary>
        /// <param name="parameter">Not used</param>
        public override void Execute(object parameter) {
            ((IMainModelView)_MV).ValidateXmlCommandExecute();
        }
    }
}